package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.input.SmsHomeAdvertiseInpDTO;
import com.cloud.mall.sms.mapper.SmsHomeAdvertiseMapper;
import com.cloud.mall.sms.model.SmsHomeAdvertise;
import com.cloud.mall.sms.model.SmsHomeAdvertiseExample;
import com.cloud.mall.sms.out.SmsHomeAdvertiseOut;
import com.cloud.mall.sms.service.SmsHomeAdvertiseService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SmsHomeAdvertiseServiceImpl implements SmsHomeAdvertiseService {
    @Autowired
    private SmsHomeAdvertiseMapper advertiseMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody SmsHomeAdvertiseInpDTO smsHomeAdvertiseInpDTO) {
        SmsHomeAdvertise advertise=MallBeanUtils.dtoToDo(smsHomeAdvertiseInpDTO, SmsHomeAdvertise.class);
        advertise.setClickCount(0);
        advertise.setOrderCount(0);
        int count=advertiseMapper.insert(advertise);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse update(Long id,@RequestBody SmsHomeAdvertiseInpDTO smsHomeAdvertiseInpDTO) {
        SmsHomeAdvertise advertise=MallBeanUtils.dtoToDo(smsHomeAdvertiseInpDTO, SmsHomeAdvertise.class);
        advertise.setId(id);
        int count=advertiseMapper.updateByPrimaryKey(advertise);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Transactional
    @Override
    public BaseResponse delete(Long id) {
        advertiseMapper.deleteByPrimaryKey(id);
        return BaseResponse.setResultSuccess();
    }

    @Override
    public BaseResponse update(Long id, Integer status) {
        SmsHomeAdvertise advertise = new SmsHomeAdvertise();
        advertise.setId(id);
        advertise.setStatus(status);
        int count=advertiseMapper.updateByPrimaryKeySelective(advertise);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<SmsHomeAdvertiseOut> getItem(Long id) {
        SmsHomeAdvertise advertise = advertiseMapper.selectByPrimaryKey(id);
        return BaseResponse.setResultSuccess(MallBeanUtils.dtoToDo(advertise,SmsHomeAdvertiseOut.class));
    }

    @Override
    public BaseResponse<CommonPage<SmsHomeAdvertiseOut>> list(String name, Integer type, String endTime, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        SmsHomeAdvertiseExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type != null) {
            criteria.andTypeEqualTo(type);
        }
        if (!StringUtils.isEmpty(endTime)) {
            String startStr = endTime + " 00:00:00";
            String endStr = endTime + " 23:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date start = null;
            try {
                start = sdf.parse(startStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Date end = null;
            try {
                end = sdf.parse(endStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (start != null && end != null) {
                criteria.andEndTimeBetween(start, end);
            }
        }
        example.setOrderByClause("sort desc");
        List<SmsHomeAdvertise> advertiseList = advertiseMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(advertiseList, SmsHomeAdvertiseOut.class)));
    }

    @Override
    public BaseResponse<List<SmsHomeAdvertiseOut>> getHomeAdvertiseList() {
        SmsHomeAdvertiseExample example = new SmsHomeAdvertiseExample();
        example.createCriteria().andTypeEqualTo(1).andStatusEqualTo(1);
        example.setOrderByClause("sort desc");
        return  BaseResponse.setResultSuccess(MallBeanUtils.copyDoToDto(advertiseMapper.selectByExample(example),SmsHomeAdvertiseOut.class));
    }


}
